package com.bgt.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;

import com.bgt.model.MUser;
import com.bgt.utils.HibernateUtils;

public class MUserDao {
	
	public MUser getUserById(String Id){
		MUser user=null;
		Session sess=HibernateUtils.getSession();
		Transaction tx= sess.beginTransaction();
		try{
			user=(MUser)sess.get(MUser.class, Id);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
			return null;
		}
		
		return user;
	};
	@SuppressWarnings("unchecked")
	public List<MUser> getUsersPage(Map<String,String> params){
		Session sess=HibernateUtils.getSession();
		String sql="select * from m_user where 1=1 ";
			if(params.get("username")!=null){
				sql+=" and username like%"+params.get("username")+"%";
			}
			sql+=" limit "+params.get("startsize")+","+params.get("pagesize");
			SQLQuery q=sess.createSQLQuery(sql);
			//q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			List<MUser> users=(List<MUser>)q.list();
			sess.close();
		return users;
	}
	public List<MUser> getUsersPage2(Map<String,String> params){
		Session sess=HibernateUtils.getSession();
		String sql=" from MUser where 1=1 ";
		if(params.get("username")!=null){
			sql+=" and userName like '%"+params.get("username")+"%' ";
		}
		if(params.get("order")!=null&&params.get("sort")!=null){
			sql+="  order by "+params.get("sort")+" "+params.get("order")+" ";
		}
		//sql+=" limit "+params.get("startsize")+","+params.get("pagesize");
		Query q=sess.createQuery(sql);
		//q.setResultTransformer(Transformers.aliasToBean(MUser.class));
		q.setFirstResult(Integer.valueOf(params.get("startsize")));
		q.setMaxResults(Integer.valueOf(params.get("pagesize")));
		List<MUser> users=(List<MUser>)q.list();
		System.out.println(users);
		sess.close();
		return users;
	}
	public MUser getUserByNameAndPwd(String Name,String pwd){
		MUser user=null;
		Session sess=HibernateUtils.getSession();
		Transaction tx= sess.beginTransaction();
		try{
			String hql = "from MUser as user where user.userName=:n and user.passWord=:p";
			Query query = sess.createQuery(hql);
			query.setString("n", Name);
			query.setString("p", pwd);
			user=(MUser)query.uniqueResult();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
			return null;
		}
		
		return user;
	};
	public void addUser(MUser user){
		Session sess=HibernateUtils.getSession();
		Transaction tx= sess.beginTransaction();
		try{
		sess.save(user);
		tx.commit();
		sess.close();
		}catch(Exception e){
			tx.rollback();
			sess.close();
			e.printStackTrace();
		}
	}
	public int countUsersPage2(Map<String, String> params) {
		Session sess=HibernateUtils.getSession();
		String sql=" from MUser where 1=1 ";
		if(params.get("username")!=null){
			sql+=" and userName like '%"+params.get("username")+"%' ";
		}
		Query q=sess.createQuery(sql);
		List<MUser> users=(List<MUser>)q.list();
		System.out.println(users);
		sess.close();
		return users.size();
	}

}
